Imports System 
Imports System.Text 
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Common
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Configuration 
Imports System.Xml 
Imports System.Xml.Serialization
Imports SubSonic 
Imports SubSonic.Utilities
Namespace Data
	''' <summary>
	''' Strongly-typed collection for the AspnetUsers class.
	''' </summary>
	<Serializable> _
	Public Partial Class AspnetUsersCollection 
	Inherits ActiveList(Of AspnetUsers, AspnetUsersCollection)
	    Public Sub New()
		End Sub
	End Class
	''' <summary>
	''' This is an ActiveRecord class which wraps the aspnet_Users table.
	''' </summary>
	<Serializable> _
	Public Partial Class AspnetUsers 
	Inherits ActiveRecord(Of AspnetUsers)
		#Region ".ctors and Default Settings"
		
		Public Sub New()
			SetSQLProps()
			InitSetDefaults()
			MarkNew()
		End Sub
		
		Public Sub New(ByVal useDatabaseDefaults As Boolean)
			SetSQLProps()
			If useDatabaseDefaults = True Then
				ForceDefaults()
			End If
			MarkNew()
		End Sub
		Public Sub New(ByVal keyID As Object)
			SetSQLProps()
			InitSetDefaults()
			LoadByKey(keyID)
		End Sub
		Public Sub New(ByVal columnName As String, ByVal columnValue As Object)
			SetSQLProps()
			InitSetDefaults()
			LoadByParam(columnName,columnValue)
		End Sub
		Private Sub InitSetDefaults()
			SetDefaults()
		End Sub
		
		Protected Shared Sub SetSQLProps()
			GetTableSchema()
		End Sub
		#End Region
		
		#Region "Schema and Query Accessor"
		
		Public Shared ReadOnly Property Schema() As TableSchema.Table
			Get
				If BaseSchema Is Nothing Then
					SetSQLProps()
				End If
				Return BaseSchema
			End Get
		End Property
		Private Shared Sub GetTableSchema()
			If (Not IsSchemaInitialized) Then
				'Schema declaration
				Dim schema As TableSchema.Table = New TableSchema.Table("aspnet_Users", TableType.Table, DataService.GetInstance("ClubStarterKitData"))
				schema.Columns = New TableSchema.TableColumnCollection()
				schema.SchemaName = "dbo"
				'columns
				
                
                Dim colvarApplicationId As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarApplicationId.ColumnName = "ApplicationId"
                colvarApplicationId.DataType = DbType.Guid
                colvarApplicationId.MaxLength = 0
                colvarApplicationId.AutoIncrement = false
                colvarApplicationId.IsNullable = false
                colvarApplicationId.IsPrimaryKey = false
                colvarApplicationId.IsForeignKey = true
                colvarApplicationId.IsReadOnly = false
                
                
				colvarApplicationId.ForeignKeyTableName = "aspnet_Applications"
                
                schema.Columns.Add(colvarApplicationId)
                
                Dim colvarUserId As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarUserId.ColumnName = "UserId"
                colvarUserId.DataType = DbType.Guid
                colvarUserId.MaxLength = 0
                colvarUserId.AutoIncrement = false
                colvarUserId.IsNullable = false
                colvarUserId.IsPrimaryKey = true
                colvarUserId.IsForeignKey = false
                colvarUserId.IsReadOnly = false
                
						colvarUserId.DefaultSetting = "(newid())"
				
                
                schema.Columns.Add(colvarUserId)
                
                Dim colvarUserName As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarUserName.ColumnName = "UserName"
                colvarUserName.DataType = DbType.String
                colvarUserName.MaxLength = 256
                colvarUserName.AutoIncrement = false
                colvarUserName.IsNullable = false
                colvarUserName.IsPrimaryKey = false
                colvarUserName.IsForeignKey = false
                colvarUserName.IsReadOnly = false
                
                
                schema.Columns.Add(colvarUserName)
                
                Dim colvarLoweredUserName As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarLoweredUserName.ColumnName = "LoweredUserName"
                colvarLoweredUserName.DataType = DbType.String
                colvarLoweredUserName.MaxLength = 256
                colvarLoweredUserName.AutoIncrement = false
                colvarLoweredUserName.IsNullable = false
                colvarLoweredUserName.IsPrimaryKey = false
                colvarLoweredUserName.IsForeignKey = false
                colvarLoweredUserName.IsReadOnly = false
                
                
                schema.Columns.Add(colvarLoweredUserName)
                
                Dim colvarMobileAlias As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarMobileAlias.ColumnName = "MobileAlias"
                colvarMobileAlias.DataType = DbType.String
                colvarMobileAlias.MaxLength = 16
                colvarMobileAlias.AutoIncrement = false
                colvarMobileAlias.IsNullable = true
                colvarMobileAlias.IsPrimaryKey = false
                colvarMobileAlias.IsForeignKey = false
                colvarMobileAlias.IsReadOnly = false
                
						colvarMobileAlias.DefaultSetting = "(NULL)"
				
                
                schema.Columns.Add(colvarMobileAlias)
                
                Dim colvarIsAnonymous As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarIsAnonymous.ColumnName = "IsAnonymous"
                colvarIsAnonymous.DataType = DbType.Boolean
                colvarIsAnonymous.MaxLength = 0
                colvarIsAnonymous.AutoIncrement = false
                colvarIsAnonymous.IsNullable = false
                colvarIsAnonymous.IsPrimaryKey = false
                colvarIsAnonymous.IsForeignKey = false
                colvarIsAnonymous.IsReadOnly = false
                
						colvarIsAnonymous.DefaultSetting = "((0))"
				
                
                schema.Columns.Add(colvarIsAnonymous)
                
                Dim colvarLastActivityDate As TableSchema.TableColumn = New TableSchema.TableColumn(schema)
                colvarLastActivityDate.ColumnName = "LastActivityDate"
                colvarLastActivityDate.DataType = DbType.DateTime
                colvarLastActivityDate.MaxLength = 0
                colvarLastActivityDate.AutoIncrement = false
                colvarLastActivityDate.IsNullable = false
                colvarLastActivityDate.IsPrimaryKey = false
                colvarLastActivityDate.IsForeignKey = false
                colvarLastActivityDate.IsReadOnly = false
                
                
                schema.Columns.Add(colvarLastActivityDate)
                
				BaseSchema = schema
				
				'add this schema to the provider
                'so we can query it later
                DataService.Providers("ClubStarterKitData").AddSchema("aspnet_Users",schema)
			End If
		End Sub
		Public Shared Function CreateQuery() As Query
			Return New Query(Schema)
		End Function
		
		#End Region
		
		#Region "Props"
	
          
        <XmlAttribute("ApplicationId")> _
        Public Property ApplicationId As Guid 
			Get
				Return GetColumnValue(Of Guid)("ApplicationId")
			End Get
		    
			Set
				SetColumnValue("ApplicationId", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("UserId")> _
        Public Property UserId As Guid 
			Get
				Return GetColumnValue(Of Guid)("UserId")
			End Get
		    
			Set
				SetColumnValue("UserId", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("UserName")> _
        Public Property UserName As String 
			Get
				Return GetColumnValue(Of String)("UserName")
			End Get
		    
			Set
				SetColumnValue("UserName", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("LoweredUserName")> _
        Public Property LoweredUserName As String 
			Get
				Return GetColumnValue(Of String)("LoweredUserName")
			End Get
		    
			Set
				SetColumnValue("LoweredUserName", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("MobileAlias")> _
        Public Property MobileAlias As String 
			Get
				Return GetColumnValue(Of String)("MobileAlias")
			End Get
		    
			Set
				SetColumnValue("MobileAlias", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("IsAnonymous")> _
        Public Property IsAnonymous As Boolean 
			Get
				Return GetColumnValue(Of Boolean)("IsAnonymous")
			End Get
		    
			Set
				SetColumnValue("IsAnonymous", Value)
			End Set
		End Property
		
		
		  
        <XmlAttribute("LastActivityDate")> _
        Public Property LastActivityDate As DateTime 
			Get
				Return GetColumnValue(Of DateTime)("LastActivityDate")
			End Get
		    
			Set
				SetColumnValue("LastActivityDate", Value)
			End Set
		End Property
		
		
		
		
		#End Region
		
		
			    
	    
	    #Region "PrimaryKey Methods"
	    
			Public Function AspnetMembershipRecords() As Data.AspnetMembershipCollection 
	
				Return New Data.AspnetMembershipCollection().Where(AspnetMembership.Columns.UserId, UserId).Load()
	
			End Function
			
			Public Function AspnetPersonalizationPerUserRecords() As Data.AspnetPersonalizationPerUserCollection 
	
				Return New Data.AspnetPersonalizationPerUserCollection().Where(AspnetPersonalizationPerUser.Columns.UserId, UserId).Load()
	
			End Function
			
			Public Function AspnetProfileRecords() As Data.AspnetProfileCollection 
	
				Return New Data.AspnetProfileCollection().Where(AspnetProfile.Columns.UserId, UserId).Load()
	
			End Function
			
			Public Function AspnetUsersInRolesRecords() As Data.AspnetUsersInRolesCollection 
	
				Return New Data.AspnetUsersInRolesCollection().Where(AspnetUsersInRoles.Columns.UserId, UserId).Load()
	
			End Function
			
		#End Region
		
		
		
		
		
		
	    
	    #Region "ForeignKey Methods"
	    
		''' <summary>
		''' Returns a AspnetApplications ActiveRecord object related to this AspnetUsers
		''' </summary>
		Public Property AspnetApplications() As Data.AspnetApplications
			Get
				Return Data.AspnetApplications.FetchByID(Me.ApplicationId)
			End Get
			Set
				SetColumnValue("ApplicationId", Value.ApplicationId)
			End Set
		End Property
	    
	    #End Region
	    
		
	    
	    #Region "Many To Many Helpers"
	    
	     
        Public Function GetAspnetRolesCollection() As Data.AspnetRolesCollection 
	        Return AspnetUsers.GetAspnetRolesCollection(Me.UserId)
		End Function
		
        Public Shared Function GetAspnetRolesCollection(ByVal varUserId As Guid) As Data.AspnetRolesCollection
        
            Dim cmd As SubSonic.QueryCommand = New SubSonic.QueryCommand("SELECT * FROM aspnet_Roles INNER JOIN aspnet_UsersInRoles ON aspnet_Roles.RoleId=aspnet_UsersInRoles.RoleId WHERE aspnet_UsersInRoles.UserId=@UserId", AspnetUsers.Schema.Provider.Name)
            
            cmd.AddParameter("@UserId", varUserId, DbType.Guid)
            Dim rdr As IDataReader = SubSonic.DataService.GetReader(cmd)
            Dim coll As AspnetRolesCollection = New AspnetRolesCollection()
            coll.LoadAndCloseReader(rdr)
            Return coll
            
        End Function
        
        Public Shared Sub SaveAspnetRolesMap(ByVal varUserId As Guid, ByVal items As AspnetRolesCollection)
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM aspnet_UsersInRoles WHERE UserId=@UserId", AspnetUsers.Schema.Provider.Name)
            cmdDel.AddParameter("@UserId", varUserId)
            'add this in
            coll.Add(cmdDel)
			DataService.ExecuteTransaction(coll)
            For Each item As AspnetRoles In items 
				Dim varAspnetUsersInRoles As AspnetUsersInRoles = New AspnetUsersInRoles()
				varAspnetUsersInRoles.SetColumnValue("UserId", varUserId)
				varAspnetUsersInRoles.SetColumnValue("RoleId", item.GetPrimaryKeyValue())
				varAspnetUsersInRoles.Save()
			Next
			
        End Sub
        
        
        Public Shared Sub SaveAspnetRolesMap(ByVal varUserId As Guid, ByVal itemList As System.Web.UI.WebControls.ListItemCollection)
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM aspnet_UsersInRoles WHERE UserId=@UserId", AspnetUsers.Schema.Provider.Name)
            cmdDel.AddParameter("@UserId", varUserId)
            'add this in
            coll.Add(cmdDel)
            DataService.ExecuteTransaction(coll)
            For Each l As System.Web.UI.WebControls.ListItem In itemList 
            
                If l.Selected
				    
					Dim varAspnetUsersInRoles As AspnetUsersInRoles = New AspnetUsersInRoles()
					varAspnetUsersInRoles.SetColumnValue("UserId", varUserId)
					varAspnetUsersInRoles.SetColumnValue("RoleId", l.Value)
					varAspnetUsersInRoles.Save()
				    
                End If
            Next
        End Sub
        
        Public Shared Sub SaveAspnetRolesMap(ByVal varUserId As Guid, ByVal itemList() As Guid) 
        
            Dim coll As QueryCommandCollection = New SubSonic.QueryCommandCollection()
            'delete out the existing
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM aspnet_UsersInRoles WHERE UserId=@UserId", AspnetUsers.Schema.Provider.Name)
            cmdDel.AddParameter("@UserId", varUserId)
            'add this in
            coll.Add(cmdDel)
			DataService.ExecuteTransaction(coll)
            
            For Each item As Guid In itemList
            
                	Dim varAspnetUsersInRoles As AspnetUsersInRoles = New AspnetUsersInRoles()
					varAspnetUsersInRoles.SetColumnValue("UserId", varUserId)
					varAspnetUsersInRoles.SetColumnValue("RoleId", item)
					varAspnetUsersInRoles.Save()
            Next
            
        End Sub
        
		Public Shared Sub DeleteAspnetRolesMap(ByVal varUserId As Guid) 
		
            Dim cmdDel As QueryCommand = New QueryCommand("DELETE FROM aspnet_UsersInRoles WHERE UserId=@UserId", AspnetUsers.Schema.Provider.Name)
            cmdDel.AddParameter("@UserId", varUserId)
            DataService.ExecuteQuery(cmdDel)
        End Sub
	      
	      
	      
	    
	    #End Region
	    
		
		#Region "ObjectDataSource support"
		
		''' <summary>
		''' Inserts a record, can be used with the Object Data Source
		''' </summary>
		Public Shared Sub Insert(ByVal varApplicationId As Guid,ByVal varUserId As Guid,ByVal varUserName As String,ByVal varLoweredUserName As String,ByVal varMobileAlias As String,ByVal varIsAnonymous As Boolean,ByVal varLastActivityDate As DateTime)
			Dim item As AspnetUsers = New AspnetUsers()
			
            item.ApplicationId = varApplicationId
            
            item.UserId = varUserId
            
            item.UserName = varUserName
            
            item.LoweredUserName = varLoweredUserName
            
            item.MobileAlias = varMobileAlias
            
            item.IsAnonymous = varIsAnonymous
            
            item.LastActivityDate = varLastActivityDate
            
			If Not System.Web.HttpContext.Current Is Nothing Then
				item.Save(System.Web.HttpContext.Current.User.Identity.Name)
			Else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name)
			End If
		End Sub
		''' <summary>
		''' Updates a record, can be used with the Object Data Source
		''' </summary>
		Public Shared Sub Update(ByVal varApplicationId As Guid,ByVal varUserId As Guid,ByVal varUserName As String,ByVal varLoweredUserName As String,ByVal varMobileAlias As String,ByVal varIsAnonymous As Boolean,ByVal varLastActivityDate As DateTime)
			Dim item As AspnetUsers = New AspnetUsers()
		    
                item.ApplicationId = varApplicationId
				
                item.UserId = varUserId
				
                item.UserName = varUserName
				
                item.LoweredUserName = varLoweredUserName
				
                item.MobileAlias = varMobileAlias
				
                item.IsAnonymous = varIsAnonymous
				
                item.LastActivityDate = varLastActivityDate
				
			item.IsNew = False
			If Not System.Web.HttpContext.Current Is Nothing Then
				item.Save(System.Web.HttpContext.Current.User.Identity.Name)
			Else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name)
			End If
		End Sub
		#End Region
		#Region "Columns Struct"
		Public Structure Columns
			Dim x as Integer
			
            Public Shared ApplicationId As String = "ApplicationId"
            
            Public Shared UserId As String = "UserId"
            
            Public Shared UserName As String = "UserName"
            
            Public Shared LoweredUserName As String = "LoweredUserName"
            
            Public Shared MobileAlias As String = "MobileAlias"
            
            Public Shared IsAnonymous As String = "IsAnonymous"
            
            Public Shared LastActivityDate As String = "LastActivityDate"
            
		End Structure
		#End Region
	End Class
End Namespace
